package aip.assignment2.service;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 * This service is contains functions that are application wide.
 */
@Stateless(name = "aip.assignment2.service.Application")
public class Application
{
	/**
	 * the application logger
	 */
	private final static Logger logger = Logger.getLogger("aip.assignment2");
	/**
	 * the application logger
	 */
	public Logger getLogger()
	{
		logger.setLevel(Level.ALL);
		return logger;
	}
	/**
	 * This method gets the current JSF context.
	 * @return the result FacesContext
	 */
	public FacesContext getFacesContext()
	{
		return FacesContext.getCurrentInstance();
	}
	/**
	 * This method gets the current JSF external context.
	 * @return the result ExternalContext
	 */
	public ExternalContext getExternalContext()
	{
		return FacesContext.getCurrentInstance().getExternalContext();
	}
	/**
	 * This method adds a certain message to the current JSF context.
	 * @param severity the severity of the message
	 * @param summary the message summary
	 * @param detail the message detail
	 */
	public void addMessage(FacesMessage.Severity severity, String summary)
	{
		if (severity != null && summary != null)
		{
			FacesMessage successMessage = new FacesMessage(severity, summary, "");
			getFacesContext().addMessage(null, successMessage);
		}
	}
	/**
	 * This method gets the current session object.
	 * @return the result HttpSession
	 */
	public HttpSession getCurrentSession()
	{
		FacesContext facesContext = FacesContext.getCurrentInstance();
		return (HttpSession) facesContext.getExternalContext().getSession(false);
	}
}
